#include "StaticObject.h"

StaticObject::StaticObject()
    : m_pMesh(NULL)
    , m_pTexture(NULL)
    , m_shape(SHAPE_BOX)
    , m_color(D3DCOLOR_XRGB(255, 255, 255))
{
}

StaticObject::~StaticObject()
{
    Cleanup();
}

bool StaticObject::Initialize(LPDIRECT3DDEVICE9 device, ObjectShape shape, const D3DXVECTOR3& size)
{
    m_shape = shape;

    // 根据形状创建网格
    HRESULT hr;
    switch (shape)
    {
    case SHAPE_BOX:
        hr = D3DXCreateBox(device, size.x, size.y, size.z, &m_pMesh, NULL);
        // Box: DX9 creates centered at origin, so center is at Y=0
        // We want bottom at Y=0, so we need bounding box relative to mesh center
        m_boundingBox = AABB(
            D3DXVECTOR3(-size.x * 0.5f, -size.y * 0.5f, -size.z * 0.5f),
            D3DXVECTOR3(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f)
        );
        break;

    case SHAPE_CYLINDER:
        hr = D3DXCreateCylinder(device, size.x, size.x, size.y, 16, 4, &m_pMesh, NULL);
        // Cylinder: DX9 creates with center at origin, bottom at -height/2
        m_boundingBox = AABB(
            D3DXVECTOR3(-size.x, -size.y * 0.5f, -size.x),
            D3DXVECTOR3(size.x, size.y * 0.5f, size.x)
        );
        break;

    case SHAPE_SPHERE:
        hr = D3DXCreateSphere(device, size.x, 16, 16, &m_pMesh, NULL);
        // Sphere: DX9 creates centered at origin
        m_boundingBox = AABB(
            D3DXVECTOR3(-size.x, -size.x, -size.x),
            D3DXVECTOR3(size.x, size.x, size.x)
        );
        break;

    default:
        return false;
    }

    if (FAILED(hr))
        return false;

    // 创建程序纹理
    hr = D3DXCreateTexture(device, 128, 128, 1, 0, D3DFMT_A8R8G8B8,
        D3DPOOL_MANAGED, &m_pTexture);

    if (SUCCEEDED(hr))
    {
        D3DLOCKED_RECT rect;
        m_pTexture->LockRect(0, &rect, NULL, 0);
        DWORD* pTexels = (DWORD*)rect.pBits;

        // Minecraft 风格的石头纹理（明显的像素化方块贴图）
        for (int y = 0; y < 128; y++)
        {
            for (int x = 0; x < 128; x++)
            {
                // 基础颜色：灰色石头
                int r = 100;
                int g = 100;
                int b = 100;

                // 16x16 像素网格（Minecraft 风格）
                bool isBorder = (x % 16 == 0 || y % 16 == 0);
                if (isBorder)
                {
                    // 边框深色
                    r -= 30;
                    g -= 30;
                    b -= 30;
                }

                // 添加随机噪点增加细节
                int noise = (rand() % 40) - 20;
                r += noise;
                g += noise;
                b += noise;

                // 确保颜色范围有效
                if (r < 50) r = 50; if (r > 180) r = 180;
                if (g < 50) g = 50; if (g > 180) g = 180;
                if (b < 50) b = 50; if (b > 180) b = 180;

                pTexels[y * 128 + x] = D3DCOLOR_ARGB(255, r, g, b);
            }
        }
        m_pTexture->UnlockRect(0);
    }

    return true;
}

void StaticObject::Render(LPDIRECT3DDEVICE9 device)
{
    if (!device || !m_pMesh)
        return;

    // 设置世界矩阵
    D3DXMATRIX matWorld, matScale, matRot, matTrans;
    D3DXMatrixScaling(&matScale, m_scale.x, m_scale.y, m_scale.z);

    D3DXMATRIX matRotX, matRotY, matRotZ;
    D3DXMatrixRotationX(&matRotX, m_rotation.x);
    D3DXMatrixRotationY(&matRotY, m_rotation.y);
    D3DXMatrixRotationZ(&matRotZ, m_rotation.z);
    matRot = matRotX * matRotY * matRotZ;

    D3DXMatrixTranslation(&matTrans, m_position.x, m_position.y, m_position.z);

    matWorld = matScale * matRot * matTrans;
    device->SetTransform(D3DTS_WORLD, &matWorld);

    // 设置材质
    D3DMATERIAL9 material = {};
    BYTE r = (m_color >> 16) & 0xFF;
    BYTE g = (m_color >> 8) & 0xFF;
    BYTE b = m_color & 0xFF;

    material.Diffuse.r = material.Ambient.r = r / 255.0f;
    material.Diffuse.g = material.Ambient.g = g / 255.0f;
    material.Diffuse.b = material.Ambient.b = b / 255.0f;
    material.Diffuse.a = material.Ambient.a = 1.0f;
    device->SetMaterial(&material);

    // 设置纹理
    if (m_pTexture)
    {
        device->SetTexture(0, m_pTexture);
        device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
        device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    }

    // 渲染网格
    m_pMesh->DrawSubset(0);
}

void StaticObject::Cleanup()
{
    if (m_pTexture)
    {
        m_pTexture->Release();
        m_pTexture = NULL;
    }
    if (m_pMesh)
    {
        m_pMesh->Release();
        m_pMesh = NULL;
    }
}
